<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");

$errMsg = "";
if(isset($_REQUEST["reqtype"]) && $_REQUEST["reqtype"] == "save"){
	$errMsg .= functionalAreaMod();
	$module = sxREQUEST("module");
	$functionalArea = sxREQUEST("functional_area");
	$newFunctionalArea = sxREQUEST("new_functional_area");
	$description = sxREQUEST("description");
	$note = sxREQUEST("note");
} else {
	if(!isset($_REQUEST["mod"])){
		$url = $_SESSION[USEROBJ]->getHistory(0, true)."&msg=".urlencode(_t("variabile 'mod' non passata")); 
		serverRedir($url);
	}
	if(!isset($_REQUEST["fa"])){
		$url = $_SESSION[USEROBJ]->getHistory(0, true)."&msg=".urlencode(_t("variabile 'fa' non passata")); 
		serverRedir($url);
	}
	$module = sxREQ('mod');
	$functionalArea = $newFunctionalArea = sxREQ('fa');
	$sSQL = "SELECT * FROM functional_area WHERE module = '$module' AND functional_area = '$functionalArea'";
	$objSDAO->query($sSQL,"mod");
	if(!$row = $objSDAO->fetch("mod")) {
		$url = $_SESSION[USEROBJ]->getHistory(0, true).
			"&msg=".urlencode(_t("functinal-area '%s' missing",$module.'.'.$functionalArea)); 
		serverRedir($url);
	}
	$description = $row['description'];
	$note = $row['note'];
}
$sSQL = "SELECT description FROM module WHERE module = '$module'";
$objSDAO->query($sSQL,"mod");
if(!$row = $objSDAO->fetch("mod")) {
	$url = $_SESSION[USEROBJ]->getHistory(0, true).
		"&msg=".urlencode(_t("module '%s' missing",$module)); 
	serverRedir($url);
}
//output HTML
$aOpz = array("kMenu"=>'pro.mod', "isReloadable"=>false);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
	function objFrmSave(){
		try{
			var objFrm = document.getElementById("objFrm");
			objFrm.submit();
		}catch(e){
			raiseError(e.description, "save new sub-module");
		}			
	}
</script>
  
<?php
echo(bodyHTML($aOpz));
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"functionalAreaMod.php\" method=\"POST\" enctype=\"multipart/form-data\">\n");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"save\">\n");
echo("<input type=\"hidden\" name=\"xsn\" value=\"".XSN."\">\n");
echo("<input type=\"hidden\" name=\"module\" id=\"module\" value=\"$module\">\n");
echo("<input type=\"hidden\" name=\"functional_area\" id=\"functional_area\" value=\"$functionalArea\">\n");
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"labl01\" width=\"10%\">Module</td>".
			"<td class=\"text01\" width=\"90%\">$module - ".$row['description']."</td>" .
		"</tr>\n</table>");
$aCmd = array(
					"conf"=>array("pag"=>"javascript:objFrmSave()",
										"title"=>_t("add new functional-area"),
										"text"=>_t("save")
										),
					"back"=>array("pag"=>$objUser->getHistory(0,false),
										"title"=>"cancel operation",
										"text"=>"cancel"
										)
					);
echo(commandsHTML($aCmd));	
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan='2'>Functiona-Area</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" width='20%'>"._t("Name")." (*)</td>" .
			"<td class=\"text01\" width='80%'>" .
			"<input class=\"inpt01\" type=\"text\" name=\"new_functional_area\" id=\"new_functional_area\" maxlength=\"32\" value=\"$newFunctionalArea\" size='30'></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Description")." (*)</td>" .
			"<td class=\"text01\" colspan='1'><input class=\"inpt01\" type=\"text\" name=\"description\" id=\"description\" maxlength=\"80\" size=\"80\" value=\"".sxHE($description)."\"></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Note")."</td>" .
			"<td class=\"text01\" colspan='1'><textarea class=\"inpt01\" name=\"note\" id=\"note\" cols=\"80\" rows=\"8\">$note</textarea></td>" .
		"</tr>\n");
echo("</table>");

echo(commandsHTML($aCmd));
echo("<div id=\"messArea\" class=\"mess01\">$errMsg</div>");
echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());

function functionalAreaMod(){
	global $objSDAO;
	$errMsg = "";
	$_REQUEST["new_functional_area"] = strtolower($_REQUEST["new_functional_area"]);
	if(ereg("[^a-z0-9_]",$_REQUEST["new_functional_area"]) or $_REQUEST["new_functional_area"]=='')
		$errMsg .= _t("name : a-z 0-9 _ allowed only").".<br>";
	if(trim($_REQUEST["description"]) == '')
		$errMsg .= _t("missing description.")."<br>";
	//check existence
	if($_REQUEST["new_functional_area"] != $_REQUEST["functional_area"]) {
		$sSQL = "SELECT count(*) as num FROM functional_area " .
				" WHERE module='".sxRAS("module")."' " .
					" AND functional_area = '".sxRAS("new_functional_area")."'";
		$objSDAO->query($sSQL,"mod");
		$row = $objSDAO->fetch("mod");
		if($row["num"] != 0)
			$errMsg .= _t("existing functional-area<br>");
	}
	if($errMsg != ""){ return $errMsg; }
	$sSQL = "UPDATE functional_area SET " .
				" description = '".sxRAS("description")."', " .
				" note = '".sxRAS("note")."'" .
				" WHERE module='".sxRAS("module")."' " .
					" AND functional_area = '".sxRAS("functional_area")."'";	
	$objSDAO->query($sSQL,"ins");
	if($_REQUEST["new_functional_area"] != $_REQUEST["functional_area"]) {
		$sSQL = "UPDATE functional_area SET functional_area = '".sxRAS("new_functional_area")."'" .
				" WHERE module='".sxRAS("module")."' " .
					" AND functional_area = '".sxRAS("functional_area")."'";
		$objSDAO->query($sSQL,"mod");
		$sSQL = "UPDATE sub_module SET functional_area = '".sxRAS("new_functional_area")."'" .
				" WHERE module='".sxRAS("module")."' " .
					" AND functional_area = '".sxRAS("functional_area")."'";
		$objSDAO->query($sSQL,"mod");
	}
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);
	exit;
}
